<?php

namespace app\common\model\Depot;

use think\Model;

class Supplier extends Model
{
    //
    protected $name = "depot_supplier";

    //指定一个自动设置的时间字段
    //开启自动写入
    protected $autoWriteTimestamp = true;

    //设置字段的名字
    protected $createTime = "createtime"; //插入的时候设置的字段名

    //禁止 写入的时间字段
    protected $updateTime = false;


    protected $append = [
        'createtime_text',
        'province_text',
        'city_text',
        'district_text'
    ];


    public function getCreateTimeTextAttr($value,$data){
        $createtime = isset($data['createtime']) ? trim($data['createtime']) : '';
        if(empty($createtime)){
            return '';
        }
        return date('Y-m-d H:i',$createtime);
    }


    // 省
    public function getProvinceTextAttr($value,$data){
        $region = empty($data['province']) ? '' : trim($data['province']);

        // 查询中文字
        return model('Region')->where(['code' => $region])->value('name');
    }

    // 市
    public function getCityTextAttr($value,$data){
        $region = empty($data['city']) ? '' : trim($data['city']);

        // 查询中文字
        return model('Region')->where(['code' => $region])->value('name');
    }

    // 区
    public function getDistrictTextAttr($value,$data){
        $region = empty($data['district']) ? '' : trim($data['district']);

        // 查询中文字
        return model('Region')->where(['code' => $region])->value('name');
    }

    // 关联查询
    public function provinces()
    {
        return $this->belongsTo('app\common\model\Region','province','code',[],'LEFT')->setEagerlyType(0);
    }

    public function citys()
    {
        return $this->belongsTo('app\common\model\Region','city','code',[],'LEFT')->setEagerlyType(0);
    }

    public function districts()
    {
        return $this->belongsTo('app\common\model\Region','district','code',[],'LEFT')->setEagerlyType(0);
    }
}
